package com.yyfax.pay.order.data.service.impl;

import java.util.List;

import com.yyfax.framework.common.Log;
import com.yyfax.framework.exception.YYSqlException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.yyfax.commons.lang.DateUtil;
import com.yyfax.framework.service.CacheService;
import com.yyfax.framework.service.impl.BaseServiceImpl;
import com.yyfax.framework.yyfax.CryptUtil;
import com.yyfax.framework.yyfax.enums.YesNoEnum;
import com.yyfax.pay.order.data.dao.UserPaymentDao;
import com.yyfax.pay.order.data.service.UserPaymentService;
import com.yyfax.pay.order.model.po.UserPaymentPO;

/**
 * 用户支付信息服务实现
 *
 * @author buyi
 * @date 2017-04-12 11:09:15
 * @since 1.0.0
 */
@Service
public class UserPaymentServiceImpl extends BaseServiceImpl implements UserPaymentService {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserPaymentDao userPaymentDao;

	public UserPaymentServiceImpl(@Qualifier("payCacheService") CacheService cacheService) {
		super(cacheService);
	}

	@Override
	public UserPaymentPO queryByIdAndMerchantCode(int id, String merchantCode) throws Exception {
		if (id <= 0 && StringUtils.isBlank(merchantCode)) {
			logger.debug("参数 'id' 不能为0，且 'merchantCode' 不能为空 ");
			return null;
		}

		return userPaymentDao.selectByIdAndMerchantCode(id, merchantCode);
	}

	@Override
	public UserPaymentPO query(String merchantCode, String userId, String paymentCode) {
		if (StringUtils.isBlank(merchantCode) || StringUtils.isBlank(userId) || StringUtils.isBlank(paymentCode)) {
			logger.debug("传参merchantCode/userId/paymentCode存在为空的值");
			return null;
		}

		return userPaymentDao.selectByMerchantCodeAndUserIdAndPaymentCode(merchantCode, userId, paymentCode);
	}

	@Override
	public int save(UserPaymentPO po) throws Exception {
		if (po == null) {
			logger.debug("参数po为空，返回-1");
			return 0;
		}

		po.setCreateTime(DateUtil.getCurrentDateTime());
		po.setState(YesNoEnum.YES.getValue());

		return userPaymentDao.insert(po);
	}

	@Override
	public List<UserPaymentPO> queryListByUserIdAndMerchantCode(String userId, String merchantCode) throws Exception {
		if (StringUtils.isBlank(userId) && StringUtils.isBlank(merchantCode)) {
			return null;
		}

		return userPaymentDao.selectListByUserIdAndMerchantCode(userId, merchantCode);
	}

	@Override
	public void modify(UserPaymentPO userPaymentPO) throws Exception {
		if (userPaymentPO == null) {
			return;
		}

		userPaymentDao.update(userPaymentPO);
	}

	@Override
	public int remove(String merchantCode, String userId, String paymentCode, String cardNo) throws Throwable {
		if (StringUtils.isBlank(merchantCode) && StringUtils.isBlank(userId) && StringUtils.isBlank(paymentCode) && StringUtils.isBlank(cardNo)) {
			return -1;
		}

		return userPaymentDao.delete(merchantCode, userId, paymentCode, CryptUtil.encrypt(cardNo));
	}

	@Override
	public int modifyPhone(int id, String phone) {
		if (id <= 0 && StringUtils.isBlank(phone)) {
			return -1;
		}

		return userPaymentDao.updatePhone(id, phone);
	}

	@Override
	public int modifyPhone(String merchantCode, String userId, String paymentCode, String newPhone) {
		return userPaymentDao.updatePhoneByMerchantAndUserIdAndPayment(merchantCode, userId, paymentCode, newPhone);
	}

	@Override
	public int saveOrUpdate(UserPaymentPO userPaymentPO) {
		final String OP = "UserPaymentServiceImpl.saveOrUpdate";
		try {
			userPaymentPO.setCreateTime(DateUtil.getCurrentDateTime());
			return userPaymentDao.insert(userPaymentPO);
		} catch (DuplicateKeyException e) {
			logger.warn(Log.op(OP).msg("保存用户支付凭证主键冲突,已存在支付扩展信息,更新支付扩展信息").kv("userPaymentPO", userPaymentPO).toString());
			return userPaymentDao.update(userPaymentPO);
		}

	}

}
